<?php
/**
 * Date: 2019/12/25
 * Time: 17:32
 * author: zhaoshilong
 */
namespace app\api\controller;

use addons\ask\model\Comment;
use app\admin\model\user\UserExpertsPrice;
use app\common\controller\Api;
use app\common\model\MoneyLog;
use app\admin\model\User;
use app\admin\model\experts\Category;
use \app\admin\model\experts\Experts;

class Search extends Api
{

    protected $noNeedLogin = [];
    protected $noNeedRight = '*';
    public function index(){
        echo '1';die;
    }
    /**
     * 首页搜索
     * */
    public function search()
    {
        $this->request->filter(['strip_tags','trim']);
        $q = $this->request->param('searchTxt');

        $userModel = DB::name('user');
        // 查询专家存在id
        $ids = $userModel
            ->where(['username'=>['like','%'.$q.'%']])
            ->column('id');
        $ids = collection($ids)->toArray(); 
        if(empty($ids)){
            $experts = [];
        }else{
            $expertsModel = DB::name('user_experts');
            $experts = $expertsModel
                    ->where(['u_uid'=>['in',$ids]])
                    ->select();
            $experts = collection($experts)->toArray();
        }
        $this->success('成功', $experts, 200);
        //根据话题专家分类查询
        $where = array(
            'status' => 1,
            'c_name' =>['like','%'.$q.'%'],
        );
        $categotyModel = DB::name('user_experts_category');
        $categoty = $categotyModel
            ->where($where)
            ->select();
        $categoty = collection($categoty)->toArray();

        $data = array(
            'experts' => $experts,
            'category' => $category
        );
        $this->success('成功', $list, 200);
    }
}